package com.academic.web.controller.file;

import com.academic.common.core.domain.AjaxResult;
import com.academic.common.utils.hdfs.HdfsUtlis;
import com.academic.system.domain.Student;
import com.academic.system.domain.SysFileInfo;
import com.academic.system.service.SysFileInfoService;
import com.academic.web.controller.scala.AcademicProcess;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/sys/file")
public class FileController {
    @Autowired
    private HdfsUtlis hdfsUtlis;

    @Autowired
    private SysFileInfoService sysFileInfoService;

    @Autowired
    private AcademicProcess academicProcess;

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("---------------------" + file.getOriginalFilename() + "----------------------");
        SysFileInfo fileInfo = new SysFileInfo();
        long l = System.currentTimeMillis();
        String path = "/upload/" + l;
        hdfsUtlis.mkdir(path);

        // 处理文件上传逻辑
        if (!file.isEmpty()) {
            try {
                // 将文件直接上传到 HDFS
                String hdfsFilePath = path + "/" + file.getOriginalFilename();
                hdfsUtlis.uploadFile(file.getInputStream(), hdfsFilePath);
                fileInfo.setCreateTime(LocalDateTime.now());
                fileInfo.setUpdateTime(LocalDateTime.now());
                fileInfo.setFileName(file.getOriginalFilename());
                fileInfo.setFilePath(hdfsFilePath);
                sysFileInfoService.save(fileInfo);

                return "文件上传成功!";
            } catch (IOException e) {
                return "文件上传失败: " + e.getMessage();
            }
        } else {
            return "文件上传失败，文件为空.";
        }
    }
    @GetMapping("/list")
    public AjaxResult list() throws Exception {
        List<SysFileInfo> list = sysFileInfoService.list(new QueryWrapper<>());
        return AjaxResult.success(list);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) throws Exception {
        SysFileInfo byId = sysFileInfoService.getById(id);
        hdfsUtlis.deleteFile(byId.getFilePath());
        sysFileInfoService.removeById(id);
        return AjaxResult.success();
    }

    @GetMapping("/getWarn/{fileId}")
    public AjaxResult getWarn(@PathVariable String fileId){
        SysFileInfo byId = sysFileInfoService.getById(fileId);
        List<Student> warn = academicProcess.getWarn(byId.getFilePath());
        return AjaxResult.success(warn);

    }
}
